package com.lnsoft.cac.server.handle.codec.frameMessage;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;
import lombok.NonNull;

import java.text.SimpleDateFormat;
import java.util.Date;

import static com.lnsoft.cac.server.handle.codec.frameMessage.Util.byteArrToString;

@Data
/**
 * 配置/状态交互---装置基本信息查询
 * */
public class Cfg_BaseInfo_Cfm {
    private String VMD_Model;           //10byte        装置型号
    private String Hardware_Version;    //4byte         硬件版本号（带小数位）四个部分，每个字节代表一段；举例：1.2.4.10       01 02 04 0A
    private String Software_Version;    //4byte         软件版本号（带小数位）字符串举例：1.2.4.10       01 02 04 0A
    private String Protocol_Version;    //4byte         通信协议版本号（带小数位）举例：1.2.4.10       01 02 04 0A
    private String Bs_Manufacturer;     //20byte        生产厂家
    private String Bs_Production_Date;  //4byte         生产日期
    private String Bs_Identifier;       //20byte        出厂编号
    private String VMD_IMSI_Number;     //15byte        SIM卡串号（ASCII码）
    private String VMD_Phone_Number;    //11byte        卡号（ASCII码）


//    public ByteBuf toByteBuf() throws Exception{
//        if (VMD_Model == null) {
//            VMD_Model = new byte[10];
//        }
//        if (VMD_Model.length != 10) {
//            throw new Exception("装置型号设置异常");
//        }
//
//        ByteBuf bf = Unpooled.buffer(92);
//        bf.writeBytes(VMD_Model);
//        bf.writeInt(Hardware_Version);
//        bf.writeInt(Software_Version);
//        bf.writeInt(Protocol_Version);
//        bf.writeBytes(Bs_Manufacturer);
//        //  TODO:生产日期字节序需进一步确认
//        bf.writeInt(Bs_Production_Date);
//        bf.writeBytes(Bs_Identifier);
//        bf.writeBytes(VMD_IMSI_Number);
//        bf.writeBytes(VMD_Phone_Number);
//        return bf;
//    }




    //  解析 [配置/状态交互---装置基本信息查询 - 确认] 帧报文内容区 到消息体中
    public static Cfg_BaseInfo_Cfm decFrameBody(@NonNull byte[] frameBody) throws Exception {
        ByteBuf bf = Unpooled.wrappedBuffer(frameBody);
        Cfg_BaseInfo_Cfm msg = new Cfg_BaseInfo_Cfm();
        int len = frameBody.length;
        if (len == 0) {
            return null;
        }
        if (len != 92) {
            throw new Exception("frameBody 数据异常");
        }
        byte[] VMD_Model = new byte[10];
        bf.getBytes(0, VMD_Model);
        msg.setVMD_Model(byteArrToString(VMD_Model).trim());

        StringBuilder sb = new StringBuilder();
        sb.append(bf.getByte(10) & 0xFF).append(".");
        sb.append(bf.getByte(11) & 0xFF).append(".");
        sb.append(bf.getByte(12) & 0xFF).append(".");
        sb.append(bf.getByte(13) & 0xFF);
        msg.setHardware_Version(sb.toString());

        StringBuilder sb1 = new StringBuilder();
        sb1.append(bf.getByte(14) & 0xFF).append(".");
        sb1.append(bf.getByte(15) & 0xFF).append(".");
        sb1.append(bf.getByte(16) & 0xFF).append(".");
        sb1.append(bf.getByte(17) & 0xFF);
        msg.setSoftware_Version(sb1.toString());

        StringBuilder sb2 = new StringBuilder();
        sb2.append(bf.getByte(18) & 0xFF).append(".");
        sb2.append(bf.getByte(19) & 0xFF).append(".");
        sb2.append(bf.getByte(20) & 0xFF).append(".");
        sb2.append(bf.getByte(21) & 0xFF);
        msg.setProtocol_Version(sb2.toString());

        byte[] Bs_Manufacturer = new byte[20];
        bf.getBytes(22, Bs_Manufacturer);
        msg.setBs_Manufacturer(byteArrToString(Bs_Manufacturer).trim());

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = new Date(bf.getUnsignedIntLE(42) * 1000);
        msg.setBs_Production_Date(sf.format(d));

        byte[] Bs_Identifier = new byte[20];
        bf.getBytes(46, Bs_Identifier);
        msg.setBs_Identifier(byteArrToString(Bs_Identifier).trim());

        byte[] VMD_IMSI_Number = new byte[15];
        bf.getBytes(66, VMD_IMSI_Number);
        msg.setVMD_IMSI_Number(byteArrToString(VMD_IMSI_Number).trim());

        byte[] VMD_Phone_Number = new byte[11];
        bf.getBytes(81, VMD_Phone_Number);
        msg.setVMD_Phone_Number(byteArrToString(VMD_Phone_Number).trim());
        return msg;
    }

}
